package sysytmRepo

import (
	"gorm.io/gorm"
	"ruoyi/domain/entity/common"
	"ruoyi/domain/entity/response"
	"ruoyi/domain/entity/system"
	"ruoyi/domain/repository"
	"ruoyi/utils"
)

type SysNoticeRepo struct {
	db *gorm.DB
}

func SysNoticeRepository(db *gorm.DB) *SysNoticeRepo {
	return &SysNoticeRepo{db}
}

var _ repository.SysNoticeRepository = &SysNoticeRepo{}

func (s *SysNoticeRepo) SelectSysNoticeList(params common.SearchTableDataParam, isPage bool) common.TableDataInfo {
	var pageNum = params.PageNum
	var pageSize = params.PageSize
	sysRoles := params.Other.(system.SysNotice)

	offset := (pageNum - 1) * pageSize
	var total int64
	var rows []system.SysNotice
	var db = s.db.Model(&rows)

	var noticeTitle = sysRoles.NoticeTitle
	if noticeTitle != "" {
		db.Where("notice_title like concat('%', ?, '%')", noticeTitle)
	}

	var noticeType = sysRoles.NoticeType
	if noticeType != "" {
		db.Where("notice_type = ?", noticeType)
	}

	var createBy = sysRoles.CreateBy
	if createBy != "" {
		db.Where("create_by like concat('%', ?, '%')", createBy)
	}

	var beginTime = params.Params.BeginTime
	var endTime = params.Params.EndTime

	if beginTime != "" {
		//Loc, _ := time.LoadLocation("Asia/Shanghai")
		//startTime1, _ := time.ParseInLocation(constants.DateFormat, beginTime, Loc)
		//endTime = endTime + " 23:59:59"
		//endTime1, _ := time.ParseInLocation(constants.TimeFormat, endTime, Loc)
		startTime1, endTime1 := utils.GetBeginAndEndTime(beginTime, endTime)
		db.Where("`sys_role`.create_time >= ?", startTime1)
		db.Where("`sys_role`.create_time <= ?", endTime1)
	}

	if err := db.Count(&total).Error; err != nil {
		return common.Fail()
	}
	if isPage {
		if err := db.Limit(pageSize).Offset(offset).Find(&rows).Error; err != nil {
			return common.Fail()
		}
	} else {
		if err := db.Find(&rows).Error; err != nil {
			return common.Fail()
		}
	}

	if rows == nil {
		return common.Fail()
	} else {
		return common.Success(rows, total)
	}

}

func (s *SysNoticeRepo) FindNoticeInfoById(noticeId string) system.SysNotice {
	var notice system.SysNotice
	err := s.db.Where("notice_id = ?", noticeId).First(&notice).Error
	if err != nil {
		panic(response.ReturnFailMsg(err.Error()))
	}
	return notice
}

func (s *SysNoticeRepo) SaveNotice(sysNotice system.SysNotice) response.Response {
	err := s.db.Model(&system.SysNotice{}).Create(&sysNotice).Error
	if err != nil {
		return response.ReturnFailMsg(err.Error())
	}
	return response.ReturnSuccess("操作成功")
}

func (s *SysNoticeRepo) UploadNotice(sysNotice system.SysNotice) response.Response {
	err := s.db.Updates(&sysNotice).Error
	if err != nil {
		return response.ReturnFailMsg(err.Error())
	}
	return response.ReturnSuccess("操作成功")
}

func (s *SysNoticeRepo) DeleteNotice(noticeIds string) response.Response {
	ids := utils.Split(noticeIds)
	for i := 0; i < len(ids); i++ {
		id := ids[i]
		err := s.db.Where("notice_id = ?", id).Delete(&system.SysNotice{}).Error
		if err != nil {
			return response.ReturnFailMsg(err.Error())
		}
	}
	return response.ReturnSuccess("操作成功")
}
